
<div class="bjui-pageContent">
    <form id="app-edit" method="post" class="form-horizontal" action="/app/edit" onkeydown="if(event.keyCode==13){return false;}">
        <input type="hidden"  class="input-nm" name="id" id="app-id" value="{{id}}" >
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label"> <span class="required-span">*</span>应用名称:
              
            </label>
            <div class="col-lg-12 col-sm-12">
                <input type="text" class="form-control input-nm" id="app-appname"  name="appname">
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label"> <span class="required-span">*</span>url:
            </label>
            <div class="col-lg-12 col-sm-12">
                <input type="text" class="form-control input-nm" id="app-url" name="url">
                <button type="button" class="field-save-btn mt-4" style="margin-left:0px" onclick="urlTest()">连接测试</button>
            </div>
            <span style="color: #FB5651;padding-left: 14px;margin-top: 4px;display: inline-block; font-size: 12px;">保存前请点击连接测试</span>
        </div>
        
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label">token:
            </label>
            <div class="col-lg-12 col-sm-12">
                <input type="text" class="form-control input-nm" id="app-token"  name="token">
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-12 col-sm-12 control-label">应用描述:</label>
            <div class="col-lg-12 col-sm-12">
           <textarea style="height: 100px" name="appmemo" id="app-appmemo" style="width: 100%" ></textarea>
            </div>
        </div>
        
    </form>
</div>
<div class="btns-group bjui-pageFooter">
    <button type="button" class="close-btn">取消</button>
    <button type="submit" disabled="true" class="save-btn app-save-btn">保存</button>
</div>

<script type="text/javascript">
       
$(function() {
        if ('{{id}}') {
            getAppInfo('{{id}}');
        }else{
             $('#app-token').val(uuid()).attr('readonly',true);
        }
        dialog_close(); 
        $('#app-edit').formValidation({            //表单
            icon: {
                valid: 'glyphicon glyphicon-ok',           //默认指示验证结果图标为null
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            }, 
            trigger:'blur',
            fields: {                                 
                url: {                            //字段
                    validators: {                     //验证器
                        notEmpty: {
                            message: '请填写网址'
                        },
                        uri:{
                          allowLocal:true,
                          protocol:'http',
                          message:'请输入http://格式网址'
                        }
                    }
                },
                token: {
                    validators: {
                        notEmpty: {
                            message: '请填写token名称'
                        }
                    }
                },
                appname: {
                    validators: {
                        notEmpty: {
                            message: '请填写应用名称'
                        },
                        regexp: {
                            regexp: /^[\u4e00-\u9fa5_a-zA-Z0-9-]{2,30}$/,
                            message: '应用名称只能由字母、数字、中文、下划线和减号组成,长度必须2到30之间'
                        }
                    }
                },
                appmemo:{
                    validators: {
                        stringLength:{
                          message:'应用描述不得超过200个字符',
                          max:200
                        }
                    }
                }

  
                }
            })
         .on('success.form.fv', function(e) {
        e.preventDefault();
        var $form = $(e.target);
        var bv = $form.data('formValidation');
        $.post($form.attr('action'), $form.serialize(), function(result) {
            if(result.statusCode=='200'){
                BJUI.alertmsg('ok',result.message);
                BJUI.dialog('closeCurrent');
                BJUI.navtab('refresh', 'sysapp');
            }else{
              BJUI.alertmsg('error',result.message);
            }
        }, 'json');
    });
 });

function getAppInfo(ids){  
        BJUI.ajax('doajax',{
          url:"/app/getApp",
          type:"post",
          okalert:false,
          dataType:"json",
          data:{"id":ids},
          okCallback:function(data){  //id发送成功向页面发送信息
            $("#app-url").val(data.data.url);
            $("#app-token").val(data.data.token).attr('readonly',true);
            $("#app-appname").val(data.data.appname);
            $("#app-appmemo").val(data.data.appmemo);
            $("#app-id").val(data.data.id);

          }
      });
   }

function urlTest(){
      var urlTest = $("[name='url']").val();
           $.ajax({
           url:"/app/testAddress",
           type:"POST",
           data:{'url':urlTest},
           dataType:"json",
            success:function(data){   //获取该网址是否可用的返回码
              BJUI.alertmsg('ok',data.message);
                if(data.statusCode=="200"){
                  BJUI.alertmsg('ok',data.message);
                  $('.app-save-btn').attr('disabled',false);
                  
                }else{
                  BJUI.alertmsg('error',data.message);
                }
             }
         });
      }




</script>
